const Koa = require('koa2');
const app = new Koa();
const Mock = require('mockjs');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');

const rule = require('./src/rule');
const {
  returnDate,
  returnImage,
  returnText,
  returnName,
  returnAddress,
  returnColor,
  returnBasic
} = rule;
//////////////--props--///////////////////////
const router = new Router();
const {
  Random
} = Mock;
let a = 0
let list = [];
let len = 200;
///////////////--data--//////////////////////
for (let i = 1; i <= len; i++) {
  let data = {
    id: i,
    author: Random.name(),
    time: Random.date(),
    img: Random.image('580x200'),
    title: Random.ctitle(10),
    content: Random.paragraph(20),
    homeAddress: Random.county(true),
  }
  list.push(data)
}
let data = {
  code: 0,
  len,
  'data': list
}
///////--get--///////--post--/////////////////
app.use(bodyParser());
app.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'content-type')
  ctx.set('Content-type', 'application/json');
  await next();
})
router.get('/', async (ctx, next) => {
  ctx.body = {
    method: ctx.method,
    msg: 'hello world'
  }
  await next();
})
router.post('/poster:counter', async (ctx, next) => {
  // console.log('.//////////.....\n',ctx.request)
  console.log(ctx.params)
  ctx.body = paramTypes(ctx.request.body, ctx.params.counter);

  await next();
})
router.get('/pubg', async (ctx, next) => {
  ctx.body = data;
  await next();
})
///////////////--routes--////////////////////
app.use(router.routes()).use(router.allowedMethods())
app.listen(1997, () => {
  console.log('port listener is 1997')
})
////////////////--func--/////////////////////
const paramTypes = (params, counter) => {
  let data = [];
  let obj = {};
  for (let i = 1; i <= counter; i++) {
    for (let [key, value] of Object.entries(params)) {
      ///format --- start
      const [type, str] = value.split('|');
      let [min, max] = str.split('->');
      console.log(min, max)
      max = max === undefined ? min : max
      ///format --- end
      obj = Object.assign({}, obj, {
        [key]: createFeild(type, min, max, ...str.split('->'))
      })
    }
    data.push(Object.assign(obj, {
      ID: i
    }))
  }
  return {
    code: 0,
    counter,
    data
  };
}

const createFeild = (type, min, max, ...imgParams) => {
  switch (type) {
    ///text
    case 'paragraph':
    case 'cparagraph':
    case "sentence":
    case "csentence":
    case "word":
    case "cword":
    case "title":
    case "ctitle":
    case "text":
      return returnText(type, min, max);
    ///image
    case 'image':
    case 'dataImage':
      return returnImage(type, ...imgParams);
    ///date
    case 'date':
    case 'time':
      return returnDate(type, min);
    ///name
    case 'name':
    case 'first':
    case 'last':
    case 'cfirst':
    case 'clast':
    case 'cname':
      return returnName(type);
    ///color
    case 'color':
    case 'hex':
    case 'rgb':
    case 'rgba':
    case 'hsl':
      return returnColor(type);
    ///address
    case 'region':
    case 'province':
    case 'city':
    case 'county':
    case 'zip':
      return returnAddress(type, min)
    ///basic
    case 'boolean':
    case 'natural':
    case 'integer':
    case 'float':
    case 'character':
    case 'string':
    case 'range':
      return returnBasic(type, ...imgParams);
    ///default
    default:
      return returnText(type, min, max);
  }
}
/////////////////////////////////////////////